package com.laicunba.service.activity.joinlistener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.ActivityJoinEvent;
import com.laicunba.pojo.ActivityJoinDO;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.UserRewardDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.UserRewardService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.StringUtil;

@Component
public class NewYearActivityListener implements ApplicationListener<ActivityJoinEvent> {
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private UserRewardService userRewardService;

	@Override
	public void onApplicationEvent(ActivityJoinEvent event) {
		ActivityJoinDO joinDO = (ActivityJoinDO) event.getSource();
		if (!"20161224".equalsIgnoreCase(joinDO.getActivityId())) {
			return;
		}

		if (joinDO != null && StringUtil.isNotEmpty(joinDO.getUserId(), joinDO.getSubActId())) {
			UserRewardDO reward = new UserRewardDO();
			reward.setActivityNum("20161224");
			reward.setRewardNum(joinDO.getSubActId());
			reward.setUserId(joinDO.getUserId());
			if ("001".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class,
						"act-new-year-red-package-20");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,
							"双旦活动积分兑换现金券");
					reward.setRewardDesc("双旦活动，积分兑换现金券");
					reward.setGiveOut(true);
					vcoinWalletService.subtract(joinDO.getUserId(), 20, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
				}
			} else if ("002".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换50元话费");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 50, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			} else if ("003".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换100元话费");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 100, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			} else if ("004".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换三只松鼠年货礼盒");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 118, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			} else if ("005".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换500元京东e卡");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 350, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			} else if ("006".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换虎牌电饭煲");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 1200, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			} else if ("007".equals(joinDO.getSubActId())) {
				reward.setRewardDesc("双旦活动，积分兑换同程旅游黄金会员卡");
				reward.setGiveOut(false);
				vcoinWalletService.subtract(joinDO.getUserId(), 1666, "NEW_YEAR", "双旦活动兑换奖品扣除积分");
			}
			userRewardService.create(reward);
		}
	}

}
